package wickethub.web.page;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.util.convert.IConverter;

import wickethub.data.DataHolder;
import wickethub.data.SearchResultProvider;
import wickethub.data.SearcherProvider;
import wickethub.data.Util;
import wickethub.domain.Resource;
import wickethub.repository.ResourceRepository;
import wickethub.web.app.EntityDetachableModel;
import wickethub.web.components.RelativeDateConverter;
import wickethub.web.components.ResourceIconImage;

import com.google.inject.Inject;

public class ListResourcePage extends BasePage {

	@Inject	private ResourceRepository repo;
	@Inject private SearcherProvider searcher;
	@Inject private DataHolder holder;
	private String query = new String();
	
	public ListResourcePage() {
		listResources();
	}
	
	public ListResourcePage(PageParameters params) {
		query = params.getString("0");
		listResources();
	}
	
	private void listResources() {
		
		final FeedbackPanel feedback = new FeedbackPanel("feedback");
		add(feedback);
		
		setTitle(new ResourceModel("main.page.title"));
		
		IDataProvider<Resource> dataProvider;
		
		if (!query.isEmpty()) {
			dataProvider = new SearchResultProvider(query, searcher, repo);
		} else {
			dataProvider = new ListDataProvider<Resource>(new ArrayList<Resource>(repo.findAllRecentResources()));
		}
		
		final DataView<Resource> repeater = new DataView<Resource>("view", dataProvider, 7) {
		    
			protected void populateItem(final Item<Resource> item) {
				
				item.setModel(new EntityDetachableModel<Resource>(item.getModelObject(), repo));
				
				final Resource resource = item.getModelObject();
		    	BookmarkablePageLink<ViewResourcePage> link = new BookmarkablePageLink<ViewResourcePage>("link", ViewResourcePage.class,
		    			new PageParameters("0=" + Util.getTextForUrl(resource.getTitle()) + ",1=" + resource.getId()));
		    	
		    	link.add(new Label("title", resource.getTitle()));
		    	item.add(link);
		    	
		    	item.add(new ResourceIconImage("image", item.getModel()));
    			
		    	item.add(Util.getRatingPanel("stars", item.getModel(), repo));
    			
		    	item.add(new Label("lastUpdated", new Model<Date>(resource.getLastUpdated())) {
    				@Override
    				public IConverter getConverter(Class<?> type) {
    					return new RelativeDateConverter();
    				}
    			});
		    	
    			item.add(new Label("tags", resource.getTopics()) {
    				@Override
    				public boolean isVisible() {
    					return !resource.getTopics().isEmpty();
    				}
    			});
    			
    			item.add(new Label("versions", resource.getVersions()) {
    				@Override
    				public boolean isVisible() {
    					return !resource.getVersions().isEmpty();
    				}
    			});
		    }
		};		
		
		
        add(new PagingNavigator("navigator", repeater));
        add(repeater);

        Form<String> form = new Form<String>("form");
		RequiredTextField<String> title = new RequiredTextField<String>("search", new PropertyModel<String>(this, "query"));
		form.add(title);
		
		Button button = new Button("submit") {
			@Override
			public void onSubmit() {
        		setResponsePage(ListResourcePage.class, new PageParameters("0=" + query));
        	}
		};
		button.setOutputMarkupId(true);
		form.add(button);
		
		add(form);        
		
		RepeatingView topics = new RepeatingView("topics");
		add(topics);
		
		List<String> toBeShuffledList = new ArrayList<String>(holder.getTopics());
		Collections.shuffle(toBeShuffledList);
		toBeShuffledList = toBeShuffledList.subList(0, (toBeShuffledList.size() < 14 ? toBeShuffledList.size() : 14)); // FIXME magic number
		
		Iterator<String> iter = toBeShuffledList.iterator();

        while (iter.hasNext()) {
            WebMarkupContainer item = new WebMarkupContainer(topics.newChildId());
            topics.add(item);
            String name = iter.next();
            Label topicName = new Label("name", name);
            BookmarkablePageLink<ListResourcePage> topicLink =
            	new BookmarkablePageLink<ListResourcePage>("link", ListResourcePage.class, new PageParameters("0=topic:" + name));
            topicLink.add(topicName);
            item.add(topicLink);
            
        }
	        
	}

}